package com.jic.product.controller;
import com.jic.common.base.vo.RestResult;
import com.jic.product.constant.consist.ResponseCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.PageInfo;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import com.jic.product.model.vo.request.MEffectProductRequest;
import com.jic.product.model.vo.response.MEffectProductResponse;
import com.jic.product.model.entity.MEffectProduct;
import com.jic.product.service.MEffectProductService;
/**
 * @Author: admin
 * @date: 2020-11-01 16:27:22
 */
@RestController
@Slf4j
@RequestMapping("/mEffectProduct")
@Api(tags = " [置顶商品表]控制类-PC")
public class MEffectProductController {
    @Autowired
    private MEffectProductService service;


    /**
     *新增记录
     * @param request 新增[置顶商品表]请求参数
     * @return MEffectProductResponse 新增[置顶商品表]响应参数
     * author
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[置顶商品表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody MEffectProductRequest request) throws Throwable {
        MEffectProduct mEffectProduct = new MEffectProduct();
        BeanUtils.copyProperties(request, mEffectProduct);
        long flag = service.save(mEffectProduct);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     * @param request 新增或修改[置顶商品表]请求参数
     * @return MEffectProductResponse 新增或修改[置顶商品表]响应参数
     * author admin
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[置顶商品表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody MEffectProductRequest request) throws Throwable {
        MEffectProduct mEffectProduct = new MEffectProduct();
        BeanUtils.copyProperties(request, mEffectProduct);
        long flag =   service.saveOrUpdate(mEffectProduct);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     * @param request 删除[置顶商品表]请求参数
     * @return MEffectProductResponse 删除[置顶商品表]响应参数
     * author admin
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[置顶商品表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody MEffectProductRequest request) throws Throwable {
        MEffectProduct mEffectProduct = new MEffectProduct();
        BeanUtils.copyProperties(request, mEffectProduct);
        long flag = service.deleteByPrimaryKeyLogically(mEffectProduct);
        return RestResult.success(flag);
    }

    @ApiOperation(value = "删除[置顶商品表]记录")
    @PostMapping("/updateStartDisable")
    @ResponseBody
    public RestResult<Long> updateStartDisable(@RequestBody MEffectProductRequest request) throws Throwable {
        long flag = service.updateStartDisable(request);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     * @param request 修改[置顶商品表]请求参数
     * @return MEffectProductResponse 修改[置顶商品表]响应参数
     * author admin
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[置顶商品表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody MEffectProductRequest request) throws Throwable {
        MEffectProduct mEffectProduct = new MEffectProduct();
        BeanUtils.copyProperties(request, mEffectProduct);
        long flag = service.updateByPrimaryKeySelective(mEffectProduct);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     * @param request 查询[置顶商品表]详情请求参数
     * @return MEffectProductResponse 查询[置顶商品表]详情响应参数
     * author admin
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[置顶商品表]详情")
    @ResponseBody
    public RestResult<MEffectProductResponse> getByPrimaryKey(@Validated  @RequestBody MEffectProductRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request!=null && request.getId()!=null) {

            MEffectProduct mEffectProduct = new MEffectProduct();
            BeanUtils.copyProperties(request, mEffectProduct);
            MEffectProduct result = service.getByPrimaryKey(mEffectProduct);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            MEffectProductResponse bo = new MEffectProductResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new MEffectProductResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<MEffectProduct>> listPage (@RequestBody MEffectProductRequest request) throws Throwable {
        request.setDeleteFlag(0);
        return RestResult.success(service.listPageBySelective(request));
    }


    /**
     *M_EFFECT_PRODUCT 查询列表（不分页）
     * @param  request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [置顶商品表] ")
    @ResponseBody
    public RestResult<List<MEffectProductResponse>>  listMEffectProductAllRecord(@RequestBody MEffectProductRequest request) throws Throwable {
        MEffectProduct param = new MEffectProduct();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<MEffectProduct> list = service.listAllRecord(param);
        List<MEffectProductResponse> responseList = list.stream().map(temp -> {
            MEffectProductResponse bo = new MEffectProductResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

}

	
